﻿////*** Guidance generated code ***//
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Net;
//using System.Net.Http;
//using System.Threading;
//using System.Threading.Tasks;
//using System.Web.Helpers;
//using System.Web.Http.Controllers;
//using System.Web.Http.Filters;

//using ProjectBase.Core;

//namespace $safeprojectname$
//{
//    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
//    public sealed class ValidateAntiForgeryTokenAttribute : FilterAttribute, IAuthorizationFilter, IActionFilter
//    {
//        public Task<HttpResponseMessage> ExecuteAuthorizationFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
//        {
//            if (actionContext.ActionDescriptor.ActionName.IsEqual("Token"))
//            {
//                return continuation();
//            }
//            try
//            {
//                var headers = actionContext.Request.Headers;
//                var cookie = headers
//                    .GetCookies()
//                    .Select(c => c[AntiForgeryConfig.CookieName])
//                    .FirstOrDefault();
//                var rvt = headers.GetValues("__RequestVerificationToken").FirstOrDefault();
//                AntiForgery.Validate(cookie != null ? cookie.Value : null, rvt);
//            }
//            catch
//            {
//                actionContext.Response = new HttpResponseMessage
//                {
//                    StatusCode = HttpStatusCode.Forbidden,
//                    RequestMessage = actionContext.ControllerContext.Request
//                };
//                return FromResult(actionContext.Response);
//            }
//            return continuation();
//        }

//        private Task<HttpResponseMessage> FromResult(HttpResponseMessage result)
//        {
//            var source = new TaskCompletionSource<HttpResponseMessage>();
//            source.SetResult(result);
//            return source.Task;
//        }

//        public Task<HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
//        {
//            if (!actionContext.ActionDescriptor.ActionName.IsEqual("Token"))
//            {
//                return continuation();
//            }
//            var response = new HttpResponseMessage
//            {
//                StatusCode = HttpStatusCode.OK,
//                RequestMessage = actionContext.ControllerContext.Request,
//            };

//            var tokenSource = AntiForgery.GetHtml().ToString().Split(new [] { "value=\"" }, StringSplitOptions.None);
//            var token = tokenSource[1].Split('"')[0];
//            response.Headers.Add("__RequestVerificationToken", token);

//            actionContext.Response = response;
//            return FromResult(actionContext.Response);
//        }
//    }

//}
